/**
 * PushButton.as
 * Keith Peters
 * version 0.9.7
 *
 * A basic button component with a label.
 *
 * Copyright (c) 2010 Keith Peters
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.feng.components
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;

	public class FButton
	{
		private static const up_frame:int = 1;

		private static const over_frame:int = 2;

		private static const down_frame:int = 3;

		private static const selected_up_frame:int = 4;

		private static const selected_over_frame:int = 5;

		private static const selected_down_frame:int = 6;

		private static const unEnabled_frame:int = 7;

		private static var buttonDic:Dictionary = new Dictionary();

		public static function make(buttonMc:MovieClip):FButton
		{
			if (buttonDic[buttonMc] == null)
			{
				buttonDic[buttonMc] = new FButton(buttonMc);
			}
			return buttonDic[buttonMc];
		}

		public static function getButton(buttonMc:MovieClip):FButton
		{
			return make(buttonMc);
		}

		protected var _buttonMc:MovieClip;

		protected var _label:FLabel;

		protected var _over:Boolean = false;

		protected var _down:Boolean = false;

		protected var _selected:Boolean = false;

		protected var _enabled:Boolean = true;

		public function FButton(buttonMc:MovieClip)
		{
			_buttonMc = buttonMc;
			init();
		}

		protected function init():void
		{
			_buttonMc.buttonMode = true;
			_buttonMc.useHandCursor = true;
			_buttonMc.gotoAndStop(up_frame);

			if (_buttonMc["labelTxt"])
				_label = FLabel.make(_buttonMc["labelTxt"]);

			_buttonMc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseGoDown);
			_buttonMc.addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
		}

		///////////////////////////////////
		// public methods
		///////////////////////////////////

		///////////////////////////////////
		// event handlers
		///////////////////////////////////

		/**
		 * Internal mouseOver handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOver(event:MouseEvent):void
		{
			_over = true;
			updateSkin();
			
			_buttonMc.addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
		}

		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOut(event:MouseEvent):void
		{
			_over = false;
			updateSkin();
			_buttonMc.removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
		}

		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoDown(event:MouseEvent):void
		{
			_down = true;
			updateSkin();

			_buttonMc.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}

		/**
		 * Internal mouseUp handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseGoUp(event:MouseEvent):void
		{
			_down = false;
			updateSkin();

			_buttonMc.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}

		protected function updateSkin():void
		{
			if (!_enabled)
			{
				_buttonMc.gotoAndStop(unEnabled_frame);
			}
			else if (_selected)
			{
				if (_down)
				{
					_buttonMc.gotoAndStop(selected_down_frame);
				}
				else if (_over)
				{
					_buttonMc.gotoAndStop(selected_over_frame);
				}
				else
				{
					_buttonMc.gotoAndStop(selected_up_frame);
				}
			}
			else
			{
				if (_down)
				{
					_buttonMc.gotoAndStop(down_frame);
				}
				else if (_over)
				{
					_buttonMc.gotoAndStop(over_frame);
				}
				else
				{
					_buttonMc.gotoAndStop(up_frame);
				}
			}
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////

		/**
		 * Sets / gets the label text shown on this Pushbutton.
		 */
		public function set label(str:String):void
		{
			if (_label)
				_label.text = str;
		}

		public function get label():String
		{
			if (_label)
				return _label.text;
			return "";
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;

			updateSkin();
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * Sets/gets whether this component is enabled or not.
		 */
		public function set enabled(value:Boolean):void
		{
			_enabled = value;
			_buttonMc.mouseEnabled = _buttonMc.mouseChildren = _enabled;
			_buttonMc.tabEnabled = value;

			updateSkin();
		}

		public function get enabled():Boolean
		{
			return _enabled;
		}

	}
}
